<?php
/**
 *
 * 功能描述（收藏数据）
 *
 * @author suoya <jlusuoya@gmail.com>
 * @version 1
 */

class ModelFavorite extends ModelBase
{
	function SaveItem($fild, $userid, $goodId)
	{
		$cnt = $this->db->Update("user_favorite", $fild, "userid = $userid and goodid = $goodId");
		return $cnt;
	}

	function NewItem($fild)
	{
		$id = $this->db->Insert("user_favorite", $fild);
		return $id;
	}

	function GetItem($userid, $goodId)
	{
		$data  = $this->db->getRow("user_favorite", 'version, updatetime', "userid = $userid and goodid = $goodId");
		return $data;
	}

	function RemoveItem($userid, $goodId)
	{
		$cnt  = $this->db->Delete("user_favorite", "userid = $userid and goodid = $goodId");
		return $cnt;
	}

	function GetList($userid, $page)
	{
		$where = "userid = $userid";
		$data  = $this->db->getRow("user_favorite", 'count(1) cnt', "1");
		if($data['cnt'] == 0)
			return array('cnt' => 0, 'pageCnt' => 0, 'data' => array());


		$goods = array('cnt' => $data['cnt'], 'pageCnt' => ceil($data['cnt'] / GOODS_PAGESIZE));
		$limit = GOODS_PAGESIZE * ($page - 1) . ", " . GOODS_PAGESIZE;
		$data  = $this->db->getAll("user_favorite", 'goodid id, name, version, updatetime', "userid = $userid", "", $limit);
		$goods['data'] = $data;
		return $goods;
	}
}

